/**
 * 密码变更管理
 * 
 * @version 1.0.0
 * @author yangyibao
 */

$(function () {
	$("#selfPwdForm").validate({
		  errorElement: 'span',
		  errorClass:'help-inline',
		  focusInvalid: false,ignore: '',
		  rules: {oldPwd: {required: true,remote:{type:'post',url:'../user/pwdVaild',data: {pwd:function(){return $("#oldPwd").val();}},dataType: "json",dataFilter:function (data) {return  data=='true'?true:false;}}},
			      newPwd:{minlength:6,required: true,maxlength:20},cancelPwd:{minlength:6,required: true,maxlength:20,equalTo:'#newPwd'},},
	      messages:{oldPwd:{remote:'原始密码不正确！'},cancelPwd:{equalTo:'两次密码输入不同!'}},
		  errorPlacement: function(error,element){error.insertAfter(element);},
		  highlight: function(element) {
			  $(element).closest('.control-group').removeClass('success');
			  $(element).closest('.help-inline').removeClass('valid');
			  $(element).closest('.help-inline').removeClass('ok');
			  $(element).closest('.control-group').addClass('error');
			  
		  },
		  unhighlight: function(element) {$(element).closest('.control-group').removeClass('error');},
		  success: function(label) {
			  label.closest('.control-group').removeClass('error');
			  label.closest('.control-group').addClass('success');
			  label.closest('.help-inline').addClass('valid ok');
			  },
		  submitHandler:function(form){
			  $.post('../user/updatePwd',{newPwd:$("#newPwd").val()},function(data){if(data.flg){succ();reset()}else{error(data.errorMsg);}});
		      return false;
		  }});
	$("#pwdBtn").click(function(){$("#selfPwdForm").submit();});
	$("#resetBtn").click(function(){reset();});
});
function reset(){
	$("#oldPwd").val('');
	$("#newPwd").val('');
	$("#cancelPwd").val('');
	$(".help-block").closest('.form-group').removeClass('has-error');
	$(".help-block").html('');
}